package com.whiteape.book.ui.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.bugly.beta.Beta;
import com.whiteape.book.R;
import com.whiteape.book.model.type.WebviewDocType;
import com.whiteape.book.ui.activity.AboutActivity;
import com.whiteape.book.ui.activity.DownloadActivity;
import com.whiteape.book.ui.activity.MainActivity;
import com.whiteape.book.ui.activity.WebviewActivity;
import com.whiteape.book.ui.base.BaseFragment;
import com.whiteape.book.utils.GlideCacheUtil;
import com.whiteape.book.utils.ShareUtil;
import com.whiteape.book.utils.StringUtils;
import com.whiteape.book.utils.ToastUtils;

import butterknife.BindView;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.rl_mine_download)
    RelativeLayout rlMineDownload;

    @BindView(R.id.rl_mine_clear_cache)
    RelativeLayout rlClearCache;

    @BindView(R.id.rl_mine_check_upgrade)
    RelativeLayout rlCheckUpgrade;

    @BindView(R.id.rl_mine_common_settings)
    RelativeLayout rlCommonSettings;

    @BindView(R.id.rl_mine_qq_group)
    RelativeLayout rlQQGroup;

    @BindView(R.id.rl_mine_about)
    RelativeLayout rlAbout;

    @BindView(R.id.mine_tv_cache_size)
    TextView tvCacheSize;

    @BindView(R.id.rl_mine_share)
    RelativeLayout rlShare;

    @BindView(R.id.rl_mine_qa)
    RelativeLayout rlQa;
    @Override
    protected int getContentId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        tvCacheSize.setText(GlideCacheUtil.getInstance().getCacheSize(getContext()));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        rlCheckUpgrade.setOnClickListener(this);
        rlClearCache.setOnClickListener(this);
        rlCommonSettings.setOnClickListener(this);
        rlAbout.setOnClickListener(this);
        rlMineDownload.setOnClickListener(this);
        rlQQGroup.setOnClickListener(this);
        rlQa.setOnClickListener(this);
        rlShare.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_mine_check_upgrade:
                Beta.checkUpgrade();
                break;
            case R.id.rl_mine_clear_cache:
                if (getContext() == null) {
                    return;
                }
                new AlertDialog.Builder(getContext())
                        .setTitle(StringUtils.getString(R.string.wa_common_tip))
                        .setMessage(R.string.sure_to_clear_cache)
                        .setTitle(StringUtils.getString(R.string.wa_common_tip))
                        .setNegativeButton(R.string.wa_common_cancel, (dialog, which) -> dialog.dismiss())
                        .setPositiveButton(R.string.wa_common_sure, ((dialog, which) -> {
                            GlideCacheUtil.getInstance().clearImageAllCache(getContext());
                            ToastUtils.show("缓存清理完毕");
                            tvCacheSize.setText("0KB");
                        }))
                        .show();
                break;
            case R.id.rl_mine_common_settings:
                break;
            case R.id.rl_mine_about:
                startActivity(new Intent(getContext(), AboutActivity.class));
                break;
            case R.id.rl_mine_qa:
                startActivity(new Intent(getContext(), WebviewActivity.class).putExtra(WebviewActivity.KEY_WEBVIEW_TYPE, WebviewDocType.WEBVIEW_DOC_TYPE_QA));
                break;
            case R.id.rl_mine_share:
                if (getActivity() != null) {
//                    ShareUtil.umShare(getActivity(), ((MainActivity) getActivity()));
                    ShareUtil.checkPermissionAndShareInFragment(this, (MainActivity)getActivity());
//                    ShareUtil.umShare();
//                    checkPermissionAndShare();
                }
                break;

            case R.id.rl_mine_download:
                startActivity(new Intent(getContext(), DownloadActivity.class));
                break;
            case R.id.rl_mine_qq_group:
                joinQQGroup("PQEP02boUOUiq_0hcdj2pO1UlybAx7zN");
                break;
        }

    }




    /****************
     *
     * 发起添加群流程。群号：白猿搜书①群(868153326) 的 key 为： PQEP02boUOUiq_0hcdj2pO1UlybAx7zN
     * 调用 joinQQGroup(PQEP02boUOUiq_0hcdj2pO1UlybAx7zN) 即可发起手Q客户端申请加群 白猿搜书①群(868153326)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回fals表示呼起失败
     ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }

}
